package com.briup.pts.service.impl;

import com.briup.pts.config.response.ResultCode;
import com.briup.pts.domain.bean.Disease;
import com.briup.pts.domain.bean.DiseaseRecord;
import com.briup.pts.domain.vo.DiseaseRecordWithDisease;
import com.briup.pts.mapper.AnimalMapper;
import com.briup.pts.mapper.DiseaseMapper;
import com.briup.pts.mapper.DiseaseRecordMapper;
import com.briup.pts.service.DiseaseRecordService;
import com.briup.pts.util.AssertUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 25095
 * @description
 * @data 2024/11/28-14:48
 */
@Service
@RequiredArgsConstructor
public class DiseaseRecordServiceImpl implements DiseaseRecordService {
    private final DiseaseRecordMapper mapper;
    private final DiseaseMapper diseaseMapper;
    private final AnimalMapper animalMapper;

    @Override
    public List<Disease> queryAllDisease() {
        return diseaseMapper.queryAll();
    }

    @Override
    public void deleteById(Integer drId) {
         mapper.deleteById(drId);
    }

    @Override
    public void deleteByIdAll(List<Integer> ids) {
        mapper.deleteByIdAll(ids);
    }

    @Override
    public void saveOrUpdate(DiseaseRecord dr) {
        // 先根据动物id查询动物，确保动物存在
        String animalId = dr.getDrAnimalId();
        int count = animalMapper.countById(animalId);
        AssertUtil.isTrue(count == 1,
                ResultCode.ANIMAL_IS_NOT_EXISTS);
        Integer drDId = dr.getDrDId();
        int count1 = diseaseMapper.countById(drDId);
        AssertUtil.isTrue(count1 == 1,
                ResultCode.DISEASE_IS_NOT_EXTSTS);

        mapper.addIndexRecord(dr);
    }

    @Override
    public PageInfo<DiseaseRecordWithDisease> pageQueryByCondition(Integer pageNum, Integer pageSize, Integer drDId, String drStatus) {
        try (Page<DiseaseRecordWithDisease> ignored = PageHelper.startPage(pageNum, pageSize)) {
            // 根据条件查询数据库中的数据
            List<DiseaseRecordWithDisease> categories = mapper.queryPage(drDId,drStatus);
            // 将查询的结果数据放入到PageInfo中返回
            return new PageInfo<>(categories);
        }
    }
}
